var net = require('net');                    
                  

function commond(host, port, word, callback){
  var client = new net.Socket();       
  var result = {
    code : 404,
    data : null,
    msg : ""
  };
  client.connect(port, host, function(){
    console.log('connected to %s:%s',host,port);
    client.write(word);    
  });                   
    
   //接受数据,回调 key \t value
  client.on('data', function (data) {                   
      console.log('DATA: ' + data);    
      result.data = data;
      result.code = 200;
      client.end(); 
      callback(null, result);
  });
  //错误回调
  client.on('error', function (exception) {              
      console.log('socket error:' + exception);
      result.code = 500;
      result.msg = exception;
      client.end();
      callback(exception, result);
  });

  client.on('end', function() {
    console.log('disconnected from server, result : %j', result);
  });  
}
commond("127.0.0.1",2181,"conf", function(e,data){
  console.log("result : %j", data);
});
commond("127.0.0.1",2181,"crst", function(e,data){
  console.log("result : %j", data);
});
commond("127.0.0.1",2181,"envi", function(e,data){
  console.log("result : %j", data);
});
commond("127.0.0.1",2181,"stat", function(e,data){
  console.log("result : %j", data);
});

/**
ZooKeeper Commands: The Four Letter Words
ZooKeeper responds to a small set of commands. Each command is composed of four letters. You issue the commands to ZooKeeper via telnet or nc, at the client port.
Three of the more interesting commands: "stat" gives some general information about the server and connected clients, while "srvr" and "cons" give extended details on server and connections respectively.
conf
New in 3.3.0: Print details about serving configuration.
cons
New in 3.3.0: List full connection/session details for all clients connected to this server. Includes information on numbers of packets received/sent, session id, operation latencies, last operation performed, etc...
crst
New in 3.3.0: Reset connection/session statistics for all connections.
dump
Lists the outstanding sessions and ephemeral nodes. This only works on the leader.
envi
Print details about serving environment
ruok
Tests if server is running in a non-error state. The server will respond with imok if it is running. Otherwise it will not respond at all.
A response of "imok" does not necessarily indicate that the server has joined the quorum, just that the server process is active and bound to the specified client port. Use "stat" for details on state wrt quorum and client connection information.
srst
Reset server statistics.
srvr
New in 3.3.0: Lists full details for the server.
stat
Lists brief details for the server and connected clients.
wchs
New in 3.3.0: Lists brief information on watches for the server.
wchc
New in 3.3.0: Lists detailed information on watches for the server, by session. This outputs a list of sessions(connections) with associated watches (paths). Note, depending on the number of watches this operation may be expensive (ie impact server performance), use it carefully.
wchp
New in 3.3.0: Lists detailed information on watches for the server, by path. This outputs a list of paths (znodes) with associated sessions. Note, depending on the number of watches this operation may be expensive (ie impact server performance), use it carefully.
mntr
New in 3.4.0: Outputs a list of variables that could be used for monitoring the health of the cluster.
**/